//app.js
App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs);
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        wx.setStorage({
          key: 'windowHeight',
          data: res.windowHeight,
        });
        that.globalData.windowHeight = res.windowHeight;
      }
    })
    this.getConfigData(that.getCode);


  },

  getCode: function () {
    var that = this;
    // 登录
    wx.login({
      success: res => {
        that.globalData.getCodeCount += 1;
        // console.log(res)
        // 发送 res.code 到后台换取 openId, sessionKey, unionId  "application/x-www-form-urlencoded"
        if (res.code) {
          let code = res.code;
          if (code == "the code is a mock one") {
            if (that.globalData.getCodeCount >= 3) {
              wx.showModal({
                title: '提示',
                content: "请升级微信到最新版本",
                showCancel: false,
                success: function (res) {
                }
              });

            } else
              that.getCode();
          } else {
            that.globalData.getCodeCount = 0;
            let url = that.getRequestUrl(that.getConfig().isFormal);
            //发起网络请求
            wx.request({
              url: url,
              header: {
                "Content-Type": "application/x-www-form-urlencoded"
              },
              method: "POST",
              data: {
                method: "CodeAutoLogin",
                code: code,
              },
              complete: function (res) {
                if (res.data) {
                  let data = res.data.data
                  if (data) {
                    if (data && data.token) {
                      that.setOpenID(data.openid);
                      that.setToken(data.token);
                      if (that.getConfig().isFormal) {
                        if (wx.reLaunch) {
                          wx.reLaunch({
                            url: "/pages/component/homes/home/home",
                          })
                        } else {
                          wx.redirectTo({
                            url: "/pages/component/homes/home/home",
                          })
                        }

                      } else {
                        if (wx.reLaunch) {
                          wx.reLaunch({
                            url: "/pages/integralmall/integralmall",
                          })
                        } else {
                          wx.redirectTo({
                            url: "/pages/integralmall/integralmall",
                          })
                        }
                      }
                    } else if (data && data.openid) {
                      that.setOpenID(data.openid);
                    }

                  }
                }
                // console.log(url, code)
              }
            })
          }
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })

  },

  //    url: 'https://shopapi.queyouquan.cn/config/config_v1.2.json',
  getConfigData: function (callback) {
    var that = this;
    wx.request({
      url: 'https://shopapi.queyouquan.cn/config/config_v1.8.json',
      header: {
        'content-type': 'application/json'
      },
      method: "GET",
      complete: function (res) {
        that.globalData.configs = res.data;
        // that.globalData.configs.isFormal = false;
        // that.globalData.configs.isShowIntegral = true;
        // that.globalData.configs.isLottery = true;
        // dev.cardshopapi.queyouquan.net  http://192.168.0.220:8010/API/Agent 内网id isWithdrawDeposit isShowIntegral    https://dev.cardshopapi.queyouquan.net 镜像id 川麻俱乐部测试ID 111586
        // that.globalData.configs.url_formal = "https://dev.cardshopapi.queyouquan.net/API/Agent.aspx";
        that.setConfig(that.globalData.configs);
        callback.call(that);
      }
    })
  },

  /**判断正式还是镜像获取相应的借口 */
  getRequestUrl(isFormal) {
    var that = this;
    if (that.getConfig()) {
      if (that.getConfig().isFormal) {
        return that.getConfig().url_formal;
      } else {
        return that.getConfig().url_debug;
      }
    } else {

    }

  },
  /**
   * 弹框
   */
  onTip(msg, url, isCloseView) {
    wx.showModal({
      title: '提示',
      content: msg,
      showCancel: false,
      success: function (res) {
        if (res.confirm) {
          if (url) {
            if (isCloseView) {

              if (wx.reLaunch) {
                wx.reLaunch({
                  url: url,
                })
              } else {
                wx.redirectTo({
                  url: url,
                })
              }

            } else {
              wx.navigateTo({
                url: url
              })
            }
          }
          // 点击确定后完成的事
        }
      }
    })
  },
  /**不做任何处理的提示框 */
  show: function (e, data) {
    let msg = "";
    if (e == "token已过期") {
      msg = "登录超时，请重新登录";
    } else
      msg = e;
    wx.showModal({
      title: "提示",
      content: msg,
      showCancel: false,
      confirmText: "确定",
      success: function (res) {
        if (res.confirm) {
          if (e == "token已过期") {
            if (wx.reLaunch) {
              wx.reLaunch({
                url: "/pages/login/login",
              })
            } else {
              wx.redirectTo({
                url: "/pages/login/login",
              })
            }
          }
        }
      }
    })
  },
  onshowModal(msg) {
    wx.showModal({
      title: '提示',
      content: msg,
      showCancel: false,
      success: function (res) {

      }
    })
  },
  getConfig() {
    return wx.getStorageSync("configs");
  },
  setConfig(configs) {
    wx.setStorageSync("configs", configs)
  },
  getToken() {
    return wx.getStorageSync("token");
  },
  setToken(token) {
    wx.setStorageSync("token", token)
  },
  getOpenID() {
    return wx.getStorageSync("openid");
  },
  setOpenID(openid) {
    wx.setStorageSync("openid", openid)
  },
  getCitysData() {
    return wx.getStorageSync("citysData");
  },
  setCitysData(citysData) {
    wx.setStorageSync("citysData", citysData)
  },
  getPhone() {
    return wx.getStorageSync("phone");
  },
  setPhone(phone) {
    wx.setStorageSync("phone", phone)
  },
  getPassword() {
    return wx.getStorageSync("password");
  },
  setPassword(password) {
    wx.setStorageSync("password", password)
  },

  globalData: {
    userInfo: null,
    configs: null,
    // token:null,
    windowHeight: 0,
    getCodeCount: 0
  }
})